package com.csdemos.algs;

import java.util.Random;

public class ArrayUtils {
	
	private static final Random rand = new Random();
	
	public static String arrayToString(int[] array) {
		StringBuilder result = new StringBuilder();
		for (int i = 0; i < array.length; ++i) {
			result.append(array[i]).append(" ");
		}
		return result.toString();
	}
	
	public static int[] stringToArray(String arrayString) {
		try {
			String[] nums = arrayString.split(" ");
			int[] result = new int[nums.length];
			for (int i = 0; i < nums.length; ++i) {
				result[i] = Integer.valueOf(nums[i]);
			}
			return result;
		} catch (Exception exception) {
			return new int[]{};
		}
		
	}
	
	public static int[] generateRandomArray(int size, int maxValue) {
		int[] result = new int[size];
		for (int i = 0; i < size; ++i) {
			result[i] = rand.nextInt(maxValue);
		}
		return result;
	}
	
	public static void swap(int[] A, int i, int j) {
		int temp = A[i];
		A[i] = A[j];
		A[j] = temp;
	}

}
